package org.computate.model.item;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Subclass;

@Cached
@Subclass
public class Photo extends Model {
	
	public String getModelDisplayName() { return "Photo"; }
	public String getModelPluralName() { return "Photos"; }
	private String uri;
	private String link;
	private Long itemId;
	private String itemName;
	private Long width;
	private Long height;
	
	public Photo() {
		super();
		setOrder("created desc");
		setPhotoPermitted(true);
	}
	
	@Override
	public void addFields() {
		super.addFields();
		
		addField("description", "Description:");
		addField("link", "Link:");
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public String getUri() {
		return uri;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getLink() {
		return link;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public Long getWidth() {
		return width;
	}

	public void setWidth(Long width) {
		this.width = width;
	}

	public Long getHeight() {
		return height;
	}

	public void setHeight(Long height) {
		this.height = height;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemName() {
		return itemName;
	}
	
	public String getUriPrefix() {
		String uri = getUri();
		String result = null;
		
		if(uri != null) {
			int index = uri.lastIndexOf('/');
			
			if(index > 0) {
				result = uri.substring(0, index + 1) + "s";
			}
		}
		
		return result;
	}
	
	public String getUriSuffix() {
		String uri = getUri();
		String result = null;
		
		if(uri != null) {
			int index = uri.lastIndexOf('/');
			
			if(index > 0) {
				result = uri.substring(index);
			}
		}
		
		return result;
	}
	
	public Double getHeightRatio() {
		Long height = getHeight();
		Long width = getWidth();
		Double result = 1D;
		
		if(height != null && width != null && height < width) {
			result = height.doubleValue() / width.doubleValue();
		}
		
		return result;
	}
	
	public Double getWidthRatio() {
		Long height = getHeight();
		Long width = getWidth();
		Double result = 1D;
		
		if(height != null && width != null && width < height) {
			result = width.doubleValue() / height.doubleValue();
		}
		
		return result;
	}
	
	public Double getHeightWidthRatio() {
		Long height = getHeight();
		Long width = getWidth();
		Double result = 1D;
		
		if(height != null && width != null) {
			result = height.doubleValue() / width.doubleValue();
		}
		
		return result;
	}
	
	public Double getWidthHeightRatio() {
		Long height = getHeight();
		Long width = getWidth();
		Double result = 1D;
		
		if(height != null && width != null) {
			result = width.doubleValue() / height.doubleValue();
		}
		
		return result;
	}
	
	public Long getMaxSize() {
		if(height > width)
			return height;
		else
			return width;
	}
}
